﻿; Crispy Plotter, a fast and easy-to-use function plotter
; Copyright (C) 2010 Konrad Kölzer

; Crispy Plotter is free software, see License.txt for details.

; Crispy Plotter Installer & Uninstaller
; Written by Konrad Kölzer
;

SetCompressor /SOLID /FINAL lzma

; --------------------------------
; Include Modern UI

	!include "MUI.nsh"
	!include "WordFunc.nsh"
	!insertmacro WordReplace

; --------------------------------
; General

	; Version, name and file
	!define VERSION "1.7.2"
	Name "Crispy Plotter ${VERSION}"
	OutFile "Crispy_Plotter_${VERSION}.exe"

	; Default installation folder
	InstallDir "$PROGRAMFILES\Crispy Plotter"

	; Get installation folder from registry if available
	InstallDirRegKey HKCU "Software\Crispy Plotter" ""

	!define DirAppData "$APPDATA\Crispy Plotter"
	!define AssocType "CrispyPlotter.Function"
	!define AssocExt ".cpf"
	

; --------------------------------
; Interface Settings
	!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
	!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
	!define MUI_COMPONENTSPAGE_SMALLDESC
	!define MUI_HEADERIMAGE
	!define MUI_HEADERIMAGE_BITMAP ".\res\Header.bmp"
	!define MUI_HEADERIMAGE_UNBITMAP ".\res\Header.bmp"
	!define MUI_WELCOMEFINISHPAGE_BITMAP ".\res\Side.bmp"
	!define MUI_UNWELCOMEFINISHPAGE_BITMAP ".\res\Side.bmp"
	!define MUI_ABORTWARNING

; --------------------------------
; Language Selection Dialog Settings

	; Remember the installer language
	!define MUI_LANGDLL_REGISTRY_ROOT "HKCU"
	!define MUI_LANGDLL_REGISTRY_KEY "Software\Crispy Plotter"
	!define MUI_LANGDLL_REGISTRY_VALUENAME "Installer Language"

; User variables
Var WorkspaceFolder ; Path of workspace folder

; --------------------------------
; Pages
	!define MUI_WELCOMEFINISHPAGE_CUSTOMFUNCTION_INIT InitInstaller

	
	!insertmacro MUI_PAGE_WELCOME
	!insertmacro MUI_PAGE_LICENSE "..\License.txt"
	!insertmacro MUI_PAGE_COMPONENTS
	!insertmacro MUI_PAGE_DIRECTORY

	; Add a directory page to let the user specify a workspace folder
	; Store the folder in $WorkspaceFolder
	!define MUI_DIRECTORYPAGE_VARIABLE $WorkspaceFolder
	!define MUI_DIRECTORYPAGE_TEXT_TOP $(LSWorkspaceTextTop)
	!define MUI_DIRECTORYPAGE_TEXT_DESTINATION $(LSWorkspaceDestination)
	!define MUI_PAGE_HEADER_TEXT $(LSWorkspaceHeaderText)
	!define MUI_PAGE_HEADER_SUBTEXT $(LSWorkspaceHeaderSubText)
	!insertmacro MUI_PAGE_DIRECTORY
	!insertmacro MUI_PAGE_INSTFILES
	!insertmacro MUI_PAGE_FINISH

	!insertmacro MUI_UNPAGE_CONFIRM
	!insertmacro MUI_UNPAGE_INSTFILES


; --------------------------------
; Languages

	!insertmacro MUI_LANGUAGE "English"
	!insertmacro MUI_LANGUAGE "German"
	!insertmacro MUI_LANGUAGE "Spanish"
    !insertmacro MUI_LANGUAGE "Portuguese"
	!insertmacro MUI_LANGUAGE "Czech"

	; Original language strings
    LangString LSLanguageCode ${LANG_ENGLISH} "en"
    
    ; Language-specific setting for manual:
    LangString LSManualLanguage ${LANG_ENGLISH} "en"
    LangString LSManualLanguage ${LANG_GERMAN} "de"
    LangString LSManualLanguage ${LANG_SPANISH} "es"
    ; Languages that have no manual, so link to an alternative language:
    LangString LSManualLanguage ${LANG_PORTUGUESE} "en"
	LangString LSManualLanguage ${LANG_CZECH} "en"
    
    ; The following strings between the BEGIN_STRINGS_TO_TRANSLATE and the
    ; END_STRINGS_TO_TRANSLATE are read by the translate_strings.py script and
    ; translated to the languages defined above. The translated strings are
    ; inserted between the BEGIN_TRANSLATED_STRINGS and END_TRANSLATED_STRINGS
    ; marks.
	; ### BEGIN_STRINGS_TO_TRANSLATE ###
	LangString LSSecMain ${LANG_ENGLISH} "Crispy Plotter (Required)"
	LangString LSSecMainDesc ${LANG_ENGLISH} "Main application files"
	LangString LSSecAssocFileType ${LANG_ENGLISH} "Associate .cpf Files"
	LangString LSSecAssocFileTypeDesc ${LANG_ENGLISH} "Associate Crispy Plotter function files (.cpf file extension) with Crispy Plotter (Recommended)."
	LangString LSSecCreateSMFolder ${LANG_ENGLISH} "Add Start Menu shortcuts"
	LangString LSSecCreateSMFolderDesc ${LANG_ENGLISH} "Add shortcut items to the Start Menu (Recommended)"
	LangString LSSecInstallExamples ${LANG_ENGLISH} "Example Functions"
	LangString LSSecInstallExamplesDesc ${LANG_ENGLISH} "Copy example functions to the workspace folder"
	LangString LSWorkspaceHeaderText ${LANG_ENGLISH} "Choose Workspace Folder"
	LangString LSWorkspaceHeaderSubText ${LANG_ENGLISH} "Chosse the folder, which is used for storing functions."
	LangString LSWorkspaceDestination ${LANG_ENGLISH} "Workspace Folder"
	LangString LSWorkspaceTextTop ${LANG_ENGLISH} "Crispy Plotter saves all functions by default in this folder. The example functions packaged with this installer will be stored there as well. To use a different folder, click Browse and select another folder."
	LangString LSDocType ${LANG_ENGLISH} "Crispy Plotter Function"
	LangString LSAppShortcut ${LANG_ENGLISH} "Crispy Plotter"
	LangString LSUninstallShortcut ${LANG_ENGLISH} "Uninstall Crispy Plotter"
	LangString LSManualShortcut ${LANG_ENGLISH} "User Manual"
	LangString LSMyFunctions ${LANG_ENGLISH} "My Functions"
	; ### END_STRINGS_TO_TRANSLATE ###
	
	; ### BEGIN_TRANSLATED_STRINGS ###	
	; Czech translation strings:
	LangString LSLanguageCode ${LANG_CZECH} "cs"
	LangString LSSecMain ${LANG_CZECH} "Crispy Plotter (požadavky)"
	LangString LSDocType ${LANG_CZECH} "Crispy Plotter funkce"
	LangString LSSecAssocFileType ${LANG_CZECH} "Asociovat .cpf soubory"
	LangString LSSecInstallExamplesDesc ${LANG_CZECH} "Zkopírujte příklad funkce pro pracovní složky"
	LangString LSAppShortcut ${LANG_CZECH} "Crispy Plotter"
	LangString LSWorkspaceHeaderSubText ${LANG_CZECH} "Vyberte složku, která se bude používat pro ukládání funkcí."
	LangString LSMyFunctions ${LANG_CZECH} "Mé funkce"
	LangString LSUninstallShortcut ${LANG_CZECH} "Uninstall Crispy Plotter"
	LangString LSSecCreateSMFolder ${LANG_CZECH} "Přidat do Start Menu zástupce"
	LangString LSSecInstallExamples ${LANG_CZECH} "Příklad funkce"
	LangString LSWorkspaceDestination ${LANG_CZECH} "Složka pracovní prostoru"
	LangString LSWorkspaceHeaderText ${LANG_CZECH} "Vyberte složky pracovního prostoru"
	LangString LSWorkspaceTextTop ${LANG_CZECH} "Crispy Plotter uloží všechny funkce ve výchozím nastavení v této složce. Funkce zabalené v instalátoru tam budou také uloženy. Chcete-li použít jiné složky, klepněte na tlačítko Procházet a zvolte jinou složku."
	LangString LSSecAssocFileTypeDesc ${LANG_CZECH} "Asociovat Crispy Plotter funkční soubory (.cpf přípona souboru) s Crispy Plotter (doporučeno)."
	LangString LSSecCreateSMFolderDesc ${LANG_CZECH} "Přidat zástupce do položky Start Menu (doporučeno)"
	LangString LSManualShortcut ${LANG_CZECH} "Uživatelská příručka"
	LangString LSSecMainDesc ${LANG_CZECH} "Soubory hlavní aplikace"
	; German translation strings:
	LangString LSLanguageCode ${LANG_GERMAN} "de"
	LangString LSSecMain ${LANG_GERMAN} "Crispy Plotter (Benötigt)"
	LangString LSDocType ${LANG_GERMAN} "Crispy Plotter Funktion"
	LangString LSSecAssocFileType ${LANG_GERMAN} "Mit .cpf-Dateien verknüpfen"
	LangString LSSecInstallExamplesDesc ${LANG_GERMAN} "Beispielfunktionen in Verzeichnis der Arbeitsumgebung kopieren"
	LangString LSAppShortcut ${LANG_GERMAN} "Crispy Plotter"
	LangString LSWorkspaceHeaderSubText ${LANG_GERMAN} "Wählen Sie das Verzeichnis aus, in dem Crispy Plotter Funktionen ablegt."
	LangString LSMyFunctions ${LANG_GERMAN} "Eigene Funktionen"
	LangString LSUninstallShortcut ${LANG_GERMAN} "Crispy Plotter deinstallieren"
	LangString LSSecCreateSMFolder ${LANG_GERMAN} "Startmenüeintrag erzeugen"
	LangString LSSecInstallExamples ${LANG_GERMAN} "Beispielfunktionen"
	LangString LSWorkspaceDestination ${LANG_GERMAN} "Verzeichnis der Arbeitsumgebung"
	LangString LSWorkspaceHeaderText ${LANG_GERMAN} "Verzeichnis der Arbeitsumgebung auswählen"
	LangString LSWorkspaceTextTop ${LANG_GERMAN} "Crispy Plotter speichert standardmäßig in diesem Verzeichnis alle Funktionen ab. Die mitgelieferten Funktionen werden ebenfalls dort installiert. Falls Sie Funktionen in einem anderen Verzeichnis speichern möchten, klicken Sie auf Durchsuchen und wählen Sie ein anderes Verzeichnis aus."
	LangString LSSecAssocFileTypeDesc ${LANG_GERMAN} "Crispy Plotter Funktionsdateien (Dateityp .cpf ) mit Crispy Plotter verknüpfen. (Empfohlen)"
	LangString LSSecCreateSMFolderDesc ${LANG_GERMAN} "Crispy Plotter Startmenüordner erzeugen"
	LangString LSManualShortcut ${LANG_GERMAN} "Handbuch"
	LangString LSSecMainDesc ${LANG_GERMAN} "Anwendungsdateien"
	; Portuguese translation strings:
	LangString LSLanguageCode ${LANG_PORTUGUESE} "pt"
	LangString LSSecMain ${LANG_PORTUGUESE} "Crispy Plotter (Necessário)"
	LangString LSDocType ${LANG_PORTUGUESE} "Função do Crispy Plotter"
	LangString LSSecAssocFileType ${LANG_PORTUGUESE} "Associar ficheiros .cpf"
	LangString LSSecInstallExamplesDesc ${LANG_PORTUGUESE} "Copiar as funções de exemplo para a pasta da Área de Trabalho"
	LangString LSAppShortcut ${LANG_PORTUGUESE} "Crispy Plotter"
	LangString LSWorkspaceHeaderSubText ${LANG_PORTUGUESE} "Escolha a pasta que é utilizada para salvaguardar funções."
	LangString LSMyFunctions ${LANG_PORTUGUESE} "As minhas funções"
	LangString LSUninstallShortcut ${LANG_PORTUGUESE} "Desinstalar Crispy Plotter"
	LangString LSSecCreateSMFolder ${LANG_PORTUGUESE} "Adicionar atalhos no Menú Iniciar"
	LangString LSSecInstallExamples ${LANG_PORTUGUESE} "Funções de exemplo"
	LangString LSWorkspaceDestination ${LANG_PORTUGUESE} "Pasta da Área de Trabalho"
	LangString LSWorkspaceHeaderText ${LANG_PORTUGUESE} "Escolha a pasta da Área de Trabalho"
	LangString LSWorkspaceTextTop ${LANG_PORTUGUESE} "Crispy Plotter salvaguarda todas as funções por definição nesta pasta. As funções de exemplo incluídas neste instalador serão também salvaguardadas nela. Para utilizar uma pasta alternativa, clique $\"Explorar$\" (Browse) e seleccione outra pasta."
	LangString LSSecAssocFileTypeDesc ${LANG_PORTUGUESE} "Associar ficheiros de funções do Crispy Plotter (ficheiros com extensão .cpf) com o Crispy Plotter (recomendado)."
	LangString LSSecCreateSMFolderDesc ${LANG_PORTUGUESE} "Adiciona atalhos ao Menú Iniciar (recomendado)"
	LangString LSManualShortcut ${LANG_PORTUGUESE} "Manual do utilizador"
	LangString LSSecMainDesc ${LANG_PORTUGUESE} "Principais ficheiros da aplicação"
	; Spanish translation strings:
	LangString LSLanguageCode ${LANG_SPANISH} "es"
	LangString LSSecMain ${LANG_SPANISH} "Crispy Plotter (necesario)"
	LangString LSDocType ${LANG_SPANISH} "Función de Crispy Plotter"
	LangString LSSecAssocFileType ${LANG_SPANISH} "Asociar archivos .cpf"
	LangString LSSecInstallExamplesDesc ${LANG_SPANISH} "Copiar funciones de ejemplo a la carpeta del espacio de trabajo"
	LangString LSAppShortcut ${LANG_SPANISH} "Crispy Plotter"
	LangString LSWorkspaceHeaderSubText ${LANG_SPANISH} "Elige la carpeta que se usará para guardar las funciones."
	LangString LSMyFunctions ${LANG_SPANISH} "Mis funciones"
	LangString LSUninstallShortcut ${LANG_SPANISH} "Desinstalar Crispy Plotter"
	LangString LSSecCreateSMFolder ${LANG_SPANISH} "Añadir accesos directos al menú de Inicio"
	LangString LSSecInstallExamples ${LANG_SPANISH} "Funciones de ejemplo"
	LangString LSWorkspaceDestination ${LANG_SPANISH} "Carpeta del espacio de trabajo"
	LangString LSWorkspaceHeaderText ${LANG_SPANISH} "Elegir carpeta del espacio de trabajo"
	LangString LSWorkspaceTextTop ${LANG_SPANISH} "Crispy Plotter guarda por defecto todas las funciones en esta carpeta. Las funciones de ejemplo incluidas en este instalador se guardarán también ahí. Para usar otra carpeta, pulsa en Examinar y elige otra carpeta."
	LangString LSSecAssocFileTypeDesc ${LANG_SPANISH} "Asocia a Crispy Plotter los archivos de función de Crispy Plotter (extensión .cpf) (recomendado)."
	LangString LSSecCreateSMFolderDesc ${LANG_SPANISH} "Añade accesos directos al menú de Inicio de Windows (recomendado)"
	LangString LSManualShortcut ${LANG_SPANISH} "Manual del usuario"
	LangString LSSecMainDesc ${LANG_SPANISH} "Archivos principales del programa"
    ; ### END_TRANSLATED_STRINGS ###

; --------------------------------
; Reserve Files

	; If you are using solid compression, files that are required before
	; the actual installation should be stored first in the data block,
	; because this will make your installer start faster.

	!insertmacro MUI_RESERVEFILE_LANGDLL


; --------------------------------
; Installer Sections

Section !$(LSSecMain) SecMain
	SectionIn RO
	; Copy program files
	SetOutPath "$INSTDIR"
	File /r /x ".svn" /x "*.po" "..\bin\Locale"
	File /r /x ".svn" "..\bin\Manual"
	File /r /x ".svn" "..\bin\Plugins"
	File "..\bin\CrispyPlotter.exe"
	File "..\License.txt"
	File "..\Contributors.txt"
	File "..\ChangeLog.txt"
	File "..\ReleaseNotes.txt"
	SetOutPath "$INSTDIR\Data"
	File "..\bin\Data\Document.ico"

	; Create workspace folder
	CreateDirectory $WorkspaceFolder
	; Create directory for application data:
	SetOutPath "${DirAppData}"
	; Write workspace folder to Settings.ini:
	FileOpen $0 "${DirAppData}\Settings.ini" w
	StrCpy $R0 $WorkspaceFolder
    ; Escape back slashes in path string:
	${WordReplace} $R0 "\" "\\" "+" $R0
	StrCpy $R0 "Workspace=$R0$\r$\n"
	FileWrite $0 $R0
    ; Write current language to Settings.ini
	StrCpy $R0 "Language=$(LSLanguageCode)$\r$\n"
	FileWrite $0 $R0
	FileClose $0

	; Store installation folder
	WriteRegStr HKCU "Software\Crispy Plotter" "" $INSTDIR
	; Create uninstaller
	WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

Section $(LSSecInstallExamples) SecInstallExamples
	SetOutPath $WorkspaceFolder
	File /r /x ".svn" /x "_Internal" "..\bin\Workspace\*.cpf"
SectionEnd

Section $(LSSecAssocFileType) SecAssocFileType
	; Back up old value of .cpf
	!define Index "Line${__LINE__}"
	ReadRegStr $1 HKCR "${AssocExt}" ""
	StrCmp $1 "" "${Index}-NoBackup"
	StrCmp $1 "${AssocType}" "${Index}-NoBackup"
	WriteRegStr HKCR "${AssocExt}" "backup_val" $1
	"${Index}-NoBackup:"
	; Save in registry , if file association has been set:
	WriteRegStr HKCU "Software\Crispy Plotter" "AssocFileType" "true"
	WriteRegStr HKCR "${AssocExt}" "" "${AssocType}"
	ReadRegStr $0 HKCR "${AssocType}" ""
	StrCmp $0 "" 0 "${Index}-Skip"
	WriteRegStr HKCR "${AssocType}" "" "$(LSDocType)"
	WriteRegStr HKCR "${AssocType}\shell" "" "open"
	WriteRegStr HKCR "${AssocType}\DefaultIcon" "" "$INSTDIR\Data\Document.ico,0"
	"${Index}-Skip:"
	WriteRegStr HKCR "${AssocType}\shell\open\command" "" \
	'"$INSTDIR\CrispyPlotter.exe" "%1"'
	System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
	!undef Index
SectionEnd

Section $(LSSecCreateSMFolder) SecCreateSMFolder
	WriteRegStr HKCU "Software\Crispy Plotter" "CreateShortcuts" "true"
	SetShellVarContext All
	CreateDirectory "$SMPROGRAMS\Crispy Plotter"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSAppShortcut).lnk" "$INSTDIR\CrispyPlotter.exe"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSUninstallShortcut).lnk" "$INSTDIR\Uninstall.exe"
	CreateShortCut "$SMPROGRAMS\Crispy Plotter\$(LSManualShortcut).lnk" "$INSTDIR\Manual\$(LSManualLanguage)\index.htm"
SectionEnd

; --------------------------------
; Installer Functions

Function InitInstaller
	StrCpy $WorkspaceFolder "$DOCUMENTS\$(LSMyFunctions)"
FunctionEnd

Function .onInit
	!insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd



; --------------------------------
; Descriptions

	; Assign language strings to sections
	!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
	!insertmacro MUI_DESCRIPTION_TEXT ${SecMain} $(LSSecMainDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecInstallExamples} $(LSSecInstallExamplesDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecAssocFileType} $(LSSecAssocFileTypeDesc)
	!insertmacro MUI_DESCRIPTION_TEXT ${SecCreateSMFolder} $(LSSecCreateSMFolder)
	!insertmacro MUI_FUNCTION_DESCRIPTION_END

; --------------------------------
; Uninstaller Section

Section "Uninstall"
	; Look in registry , if file association has been set by installer:
	ReadRegStr $1 HKCU "Software\Crispy Plotter" "AssocFileType"
	StrCmp $1 "true" 0 skipRemoveFileAssoc
	;start of restore script
	!define Index "Line${__LINE__}"
	ReadRegStr $1 HKCR "${AssocExt}" ""
	StrCmp $1 "${AssocType}" 0 "${Index}-NoOwn" ; only do this if we own it
	ReadRegStr $1 HKCR "${AssocExt}" "backup_val"
	StrCmp $1 "" 0 "${Index}-Restore" ; if backup="" then delete the whole key
	DeleteRegKey HKCR "${AssocExt}"
	Goto "${Index}-NoOwn"
	"${Index}-Restore:"
	WriteRegStr HKCR "${AssocExt}" "" $1
	DeleteRegValue HKCR "${AssocExt}" "backup_val"

	"${Index}-NoOwn:"
	DeleteRegKey HKCR "${AssocType}" ;Delete key with association settings
	System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'

	!undef Index
	;rest of script
	skipRemoveFileAssoc:

	; Remove shortcuts
	ReadRegStr $1 HKCU "Software\Crispy Plotter" "CreateShortcuts"
	StrCmp $1 "true" 0 skipRemoveShortcuts
	SetShellVarContext all
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSAppShortcut).lnk"
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSUninstallShortcut).lnk"
	Delete "$SMPROGRAMS\Crispy Plotter\$(LSManualShortcut).lnk"
	RMDir "$SMPROGRAMS\Crispy Plotter"
	skipRemoveShortcuts:

	; Remove files
	Delete "$INSTDIR\Uninstall.exe"
	Delete "$INSTDIR\Locale\*\LC_MESSAGES\default.mo"
	RMDir "$INSTDIR\Locale"

	Delete "$INSTDIR\Manual\de\res\*.*"
	RMDir "$INSTDIR\Manual\de\res"
	Delete "$INSTDIR\Manual\de\*.htm"
	RMDir "$INSTDIR\Manual\de"

	Delete "$INSTDIR\Manual\en\res\*.*"
	RMDir "$INSTDIR\Manual\en\res"
	Delete "$INSTDIR\Manual\en\*.htm"
	RMDir "$INSTDIR\Manual\en"

	Delete "$INSTDIR\Manual\es\res\*.*"
	RMDir "$INSTDIR\Manual\es\res"
	Delete "$INSTDIR\Manual\es\*.htm"
	RMDir "$INSTDIR\Manual\es"

	RMDir "$INSTDIR\Manual"

	Delete "$INSTDIR\Plugins\WaveOut\WaveOut.dll"
    RMDir "$INSTDIR\Plugins\WaveOut\"
	Delete "$INSTDIR\Plugins\FileDump\FileDump.dll"
    RMDir "$INSTDIR\Plugins\FileDump\"
	RMDir "$INSTDIR\Plugins"

	Delete "$INSTDIR\Data\Document.ico"
	RMDir "$INSTDIR\Data"
	
	; Switch to current user to clean the application folder
	SetShellVarContext current
	Delete "${DirAppData}\Settings.ini"
	Delete "${DirAppData}\Forms.ini"
	RMDir "${DirAppData}"
	

	Delete "$INSTDIR\CrispyPlotter.exe"
	Delete "$INSTDIR\License.txt"
	Delete "$INSTDIR\Contributors.txt"
	Delete "$INSTDIR\ChangeLog.txt"
	Delete "$INSTDIR\ReleaseNotes.txt"
	RMDir "$INSTDIR"

	DeleteRegKey /ifempty HKCU "Software\Crispy Plotter"
SectionEnd

; --------------------------------
; Uninstaller Functions

Function un.onInit

	!insertmacro MUI_UNGETLANGUAGE

FunctionEnd